package com.shengzai.hbase;




import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 47BE1E866CFC071DB19D5E1C056BE28AE24C16E7
 * 20180503211049
 * 20180503210349
 * 8320113
 * 118.908
 * 32.063
 * 2F3096801C8B19F6DFB82438C9A5E837
 * 118905032060040,3,3,ddr
 */
public class Hdfs2Hdfs {

    public class Hdfs2HdfsMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>{

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split(",");
            String phoneNumber = split[0];
            String startTime = split[1];
            String endTime = split[2];
            String countyId = split[3];
            String longitude = split[4];
            String latitude = split[5];

            String rowKey = phoneNumber + "|" + startTime;

            ImmutableBytesWritable immutableBytesWritable = new ImmutableBytesWritable(rowKey.getBytes());
        // KeyValue(byte[] row, byte[] family, byte[] qualifier, byte[] value)
            KeyValue valuePhoneNumber = new KeyValue(
                    rowKey.getBytes(),
                    "dianXinInfo".getBytes(),
                    "phoneNumber".getBytes(),
                    phoneNumber.getBytes()
            );
            KeyValue valueStartTime = new KeyValue(
                    rowKey.getBytes(),
                    "dianXinInfo".getBytes(),
                    "phone_number".getBytes(),
                    startTime.getBytes()
            );
            KeyValue valueEndTime = new KeyValue(
                    rowKey.getBytes(),
                    "dianXinInfo".getBytes(),
                    "phone_number".getBytes(),
                    endTime.getBytes()
            );
            KeyValue valueCountyId = new KeyValue(
                    rowKey.getBytes(),
                    "dianXinInfo".getBytes(),
                    "county_Id".getBytes(),
                    countyId.getBytes()
            );
            KeyValue valueLongiuder = new KeyValue(
                    rowKey.getBytes(),
                    "dianXinInfo".getBytes(),
                    "longitude".getBytes(),
                    longitude.getBytes()
            );
            KeyValue valueLatitude = new KeyValue(
                    rowKey.getBytes(),
                    "dianXinInfo".getBytes(),
                    "latitude".getBytes(),
                    latitude.getBytes()
            );

            context.write(immutableBytesWritable,valuePhoneNumber);
            context.write(immutableBytesWritable,valueStartTime);
            context.write(immutableBytesWritable,valueEndTime);
            context.write(immutableBytesWritable,valueCountyId);
            context.write(immutableBytesWritable,valueLongiuder);
            context.write(immutableBytesWritable,valueLatitude);

        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","master,node1,node2");
        Connection conn = ConnectionFactory.createConnection(conf);

        Job job = Job.getInstance(conf);
        job.setJobName("Hdfs2Hdfs");
        job.setJarByClass(Hdfs2Hdfs.class);

        job.setMapperClass(Hdfs2HdfsMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        job.setReducerClass(KeyValueSortReducer.class);
        job.setOutputKeyClass(ImmutableBytesWritable.class);
        job.setOutputValueClass(KeyValue.class);

        FileInputFormat.addInputPath(job,new Path("/input/DIANXIN.csv"));


        TableName tableName = TableName.valueOf("api_test:dianxin");

        Table table = conn.getTable(tableName);
        RegionLocator regionLocator = conn.getRegionLocator(tableName);

        HFileOutputFormat2.configureIncrementalLoad(job,table,regionLocator);

        Path path = new Path("/data/data2");

        Admin admin = conn.getAdmin();
        HFileOutputFormat2.setOutputPath(job,path);

        job.waitForCompletion(true);

        LoadIncrementalHFiles loadIncrementalHFiles = new LoadIncrementalHFiles(conf);

        loadIncrementalHFiles.doBulkLoad(
                path,admin,table,regionLocator
        );


    }
}
